package com.kushuigu.controller;

import com.kushuigu.domain.Address;
import com.kushuigu.domain.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Controller
public class UserController {
    @RequestMapping("/save")
    @ResponseBody
    public String save() {
        System.out.println("user save.");
        return "{'module':'user save'}";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public String delete() {
        System.out.println("user delete.");
        return "{'module':'user delete'}";
    }

    @RequestMapping("/commonParam")
    @ResponseBody
    public String commonParam(String name) {
        System.out.println("name:" + name);
        return "{'module':'user commonParam'}";
    }

    @RequestMapping("/pojoParam")
    @ResponseBody
    public String pojoParam(User user) {
        System.out.println("User:" + user);
        return "{'module':'user pojoParam'}";
    }

    @RequestMapping("/arrayParam")
    @ResponseBody
    public String arrayParam(String[] likes) {
        System.out.println("likes:" + Arrays.toString(likes));
        return "{'module':'user arrayParam'}";
    }

    @RequestMapping("/listParamForJson")
    @ResponseBody
    public String listParamForJson(@RequestBody List<String> likes) {
        System.out.println("likes:" + likes);
        return "{'module':'user listParamForJson'}";
    }

    @RequestMapping("/dateParam")
    @ResponseBody
    public String dateParam(Date date,
                            @DateTimeFormat(pattern = "yyyy-MM-dd") Date date1,
                            @DateTimeFormat(pattern = "yyyy/MM/dd HH:mm:ss") Date date2) {
        System.out.println("date:" + date);
        System.out.println("date1:" + date1);
        System.out.println("date2:" + date2);
        return "{'module':'user dateParam'}";
    }

    @RequestMapping("/toJumpPage")
    public String toJumpPage() {
        System.out.println("jump");
        return "page.jsp";
    }

    @RequestMapping("/toJsonPojo")
    @ResponseBody
    public User toJsonPojo() {
        System.out.println("pojo");
        return new User("yulei",15,null );
    }

    @RequestMapping("/toJsonPojoList")
    @ResponseBody
    public List<User> toJsonPojoList() {
        System.out.println("pojo");
        List<User> list = new ArrayList<>();
        list.add(new User("yulei",15,null ));
        list.add(new User("yulei",15,new Address("123","123") ));
        return list;
    }
}
